/**查询供货商*/
import supplierData from "./supplierData";

export const initPageInfo = () => {
  //查询参数
  supplierData.tParam.searchParam.tag = "";
  supplierData.tParam.searchParam.shopName = "";
  supplierData.tParam.searchParam.platform = "";
  supplierData.tParam.searchParam.level = null;
  supplierData.tParam.searchParam.hasBanned = null;

  //分页
  supplierData.tPage.listPage.pageNum = 1;
  supplierData.tPage.listPage.pageSize = 40;
  supplierData.tPage.listPage.total = 0;

  //数据
  supplierData.tData.list.splice(0);
}

export const querySupplierList = (self) => {
  let params = supplierData.tParam.searchParam;
  params.pageNum = supplierData.tPage.listPage.pageNum;
  params.pageSize = supplierData.tPage.listPage.pageSize;
  self.$Spin.show();
  self.$apiImpProduct.querySupplierList(params).then(response => {
    self.$Spin.hide();
    supplierData.tData.list.splice(0);
    if(response.success) {
      let page = response.result;
      supplierData.tData.list.push(...page.list);
      supplierData.tPage.listPage.total = page.total;
    }else{
      supplierData.tPage.listPage.total = 0;
      self.$Message.error(response.errorMessage);
    }
  })
}

export const addSupplier = (self) => {
  self.$refs.addForm.validate(valid => {
    if(valid) {
      let params = {shop: supplierData.tParam.addParam};
      self.$Spin.show();
      self.$apiImpProduct.addSupplier(params).then(response => {
        self.$Spin.hide();
        if(response.success) {
          self.$Message.success("新增成功！");
          querySupplierList(self);
          supplierData.tStatus.addModal.isShow = false;
        }else{
          self.$Message.error(response.errorMessage);
        }
      })
    }else{
      console.log("validate failed")
    }
  })
}

/**更新供货商信息*/
export const updateSupplier = (self) => {
  self.$refs.modifyForm.validate(valid => {
    if(valid) {
      let params = {shop: supplierData.tParam.modifyParam};
      self.$Spin.show();
      self.$apiImpProduct.updateSupplier(params).then(response => {
        self.$Spin.hide();
        if(response.success) {
          self.$Message.success("修改成功！");
          querySupplierList(self);
          supplierData.tStatus.modifyModal.isShow = false;
        }else{
          self.$Message.error(response.errorMessage);
        }
      })
    }else{
      console.log("validate failed")
    }
  })
}

/**删除供货商*/
export const deleteSupplier = (self, id) => {
  let params = {id: id};
  self.$Spin.show();
  self.$apiImpProduct.deleteSupplier(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("删除成功");
      querySupplierList(self);
    }else{
      self.$Message.error(response.errorMessage);
    }
  })
}

/**将供货商加入黑名单*/
export const addToBlacklist = (self) => {
  let params = supplierData.tParam.banParam;
  self.$Spin.show();
  self.$apiImpProduct.addSupplierToBlacklist(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("操作成功！");
      supplierData.tStatus.banModal.isShow = false;
      querySupplierList(self);
    }else{
      self.$Message.error(response.errorMessage);
    }
  })
}

/**将供货商从黑名单移除*/
export const removeFromBlacklist = (self, id) => {
  let params = {id: id};
  self.$Spin.show();
  self.$apiImpProduct.removeSupplierFromBlacklist(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("操作成功！");
      querySupplierList(self);
    }else{
      self.$Message.error(response.errorMessage);
    }
  })
}
